<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>PyPNG Code Examples &mdash; PyPNG v0.0.11 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.0.11',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="PyPNG v0.0.11 documentation" href="index.html" />
    <link rel="next" title="The png Module" href="png.html" />
    <link rel="prev" title="Why Use PyPNG?" href="ca.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="png.html" title="The png Module"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="ca.html" title="Why Use PyPNG?"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">PyPNG v0.0.11 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="pypng-code-examples">
<h1>PyPNG Code Examples<a class="headerlink" href="#pypng-code-examples" title="Permalink to this headline">¶</a></h1>
<p>This section discusses some example Python programs that use the png
module for reading and writing PNG files.</p>
<div class="section" id="writing">
<h2>Writing<a class="headerlink" href="#writing" title="Permalink to this headline">¶</a></h2>
<p>The basic strategy is to create a <a title="png.Writer" class="reference external" href="png.html#png.Writer"><tt class="xref docutils literal"><span class="pre">Writer</span></tt></a> object (instance of
<a title="png.Writer" class="reference external" href="png.html#png.Writer"><tt class="xref docutils literal"><span class="pre">png.Writer</span></tt></a>) and then call its <tt class="xref docutils literal"><span class="pre">png.write()</span></tt> method
with an open (binary) file, and the pixel data.  The <tt class="xref docutils literal"><span class="pre">Writer</span></tt> object
encapsulates all the information about the PNG file: image size, colour,
bit depth, and so on.</p>
<div class="section" id="a-ramp">
<h3>A Ramp<a class="headerlink" href="#a-ramp" title="Permalink to this headline">¶</a></h3>
<p>Create a one row image, that has all grey values from 0 to 255.  This is
a bit like Netpbm&#8217;s <tt class="docutils literal"><span class="pre">pgmramp</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">png</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;ramp.png&#39;</span><span class="p">,</span> <span class="s">&#39;wb&#39;</span><span class="p">)</span>      <span class="c"># binary mode is important</span>
<span class="n">w</span> <span class="o">=</span> <span class="n">png</span><span class="o">.</span><span class="n">Writer</span><span class="p">(</span><span class="mf">255</span><span class="p">,</span> <span class="mf">1</span><span class="p">,</span> <span class="n">greyscale</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">w</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">[</span><span class="nb">range</span><span class="p">(</span><span class="mf">256</span><span class="p">)])</span>
<span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Note that our single row, generated by <tt class="docutils literal"><span class="pre">range(256)</span></tt>, must itself be
enclosed in a list.  That&#8217;s because the <tt class="xref docutils literal"><span class="pre">png.write()</span></tt> method expects
a list of rows.</p>
<p>From now on <tt class="docutils literal"><span class="pre">import</span> <span class="pre">png</span></tt> will not be mentioned.</p>
</div>
<div class="section" id="a-little-message">
<h3>A Little Message<a class="headerlink" href="#a-little-message" title="Permalink to this headline">¶</a></h3>
<p>A list of strings holds a graphic in ASCII graphic form.  We convert it
to a list of integer lists (the required form for the <tt class="xref docutils literal"><span class="pre">write()</span></tt> method),
and write it out as a black-and-white PNG (bilevel greyscale).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">s</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;110010010011&#39;</span><span class="p">,</span>
     <span class="s">&#39;101011010100&#39;</span><span class="p">,</span>
     <span class="s">&#39;110010110101&#39;</span><span class="p">,</span>
     <span class="s">&#39;100010010011&#39;</span><span class="p">]</span>
<span class="n">s</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">x</span><span class="p">),</span> <span class="n">s</span><span class="p">)</span>

<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;png.png&#39;</span><span class="p">,</span> <span class="s">&#39;wb&#39;</span><span class="p">)</span>
<span class="n">w</span> <span class="o">=</span> <span class="n">png</span><span class="o">.</span><span class="n">Writer</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="mf">0</span><span class="p">]),</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">),</span> <span class="n">greyscale</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">bitdepth</span><span class="o">=</span><span class="mf">1</span><span class="p">)</span>
<span class="n">w</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Note how we use <tt class="docutils literal"><span class="pre">len(s[0])</span></tt> (the length of the first row) for the <em>x</em>
argument and <tt class="docutils literal"><span class="pre">len(s)</span></tt> (the number of rows) for the <em>y</em> argument.</p>
</div>
<div class="section" id="a-palette">
<h3>A Palette<a class="headerlink" href="#a-palette" title="Permalink to this headline">¶</a></h3>
<p>The previous example, &#8220;a little message&#8221;, can be converted to colour
simply by creating a PNG file with a palette.  The only difference is
that a <em>palette</em> argument is passed to the <tt class="xref docutils literal"><span class="pre">write()</span></tt> method instead of
<tt class="docutils literal"><span class="pre">greyscale=True</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Assume f and s have been set up as per previous example</span>
<span class="n">palette</span><span class="o">=</span><span class="p">[(</span><span class="mf">0</span><span class="n">x55</span><span class="p">,</span><span class="mf">0</span><span class="n">x55</span><span class="p">,</span><span class="mf">0</span><span class="n">x55</span><span class="p">),</span> <span class="p">(</span><span class="mf">0</span><span class="n">xff</span><span class="p">,</span><span class="mf">0</span><span class="n">x99</span><span class="p">,</span><span class="mf">0</span><span class="n">x99</span><span class="p">)]</span>
<span class="n">w</span> <span class="o">=</span> <span class="n">png</span><span class="o">.</span><span class="n">Writer</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="mf">0</span><span class="p">]),</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">),</span> <span class="n">palette</span><span class="o">=</span><span class="n">palette</span><span class="p">,</span> <span class="n">bitdepth</span><span class="o">=</span><span class="mf">1</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that the palette consists of two entries (the bit depth is 1 so
there are only 2 possible colours).  Each entry is an RGB triple.  If we
wanted transparency then we can use RGBA 4-tuples for each palette
entry.</p>
</div>
<div class="section" id="colour">
<h3>Colour<a class="headerlink" href="#colour" title="Permalink to this headline">¶</a></h3>
<p>For colour images the input rows are generally 3 times as long as
for greyscale, because there are 3 channels, RGB, instead of just
one, grey.  Below, the <em>p</em> literal has 2 rows of 9 values (3 RGB
pixels per row).  The spaces are just for your benefit, to mark out
the separate pixels; they have no meaning in the code.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">p</span> <span class="o">=</span> <span class="p">[(</span><span class="mf">255</span><span class="p">,</span><span class="mf">0</span><span class="p">,</span><span class="mf">0</span><span class="p">,</span> <span class="mf">0</span><span class="p">,</span><span class="mf">255</span><span class="p">,</span><span class="mf">0</span><span class="p">,</span> <span class="mf">0</span><span class="p">,</span><span class="mf">0</span><span class="p">,</span><span class="mf">255</span><span class="p">),</span>
     <span class="p">(</span><span class="mf">128</span><span class="p">,</span><span class="mf">0</span><span class="p">,</span><span class="mf">0</span><span class="p">,</span> <span class="mf">0</span><span class="p">,</span><span class="mf">128</span><span class="p">,</span><span class="mf">0</span><span class="p">,</span> <span class="mf">0</span><span class="p">,</span><span class="mf">0</span><span class="p">,</span><span class="mf">128</span><span class="p">)]</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;swatch.png&#39;</span><span class="p">,</span> <span class="s">&#39;wb&#39;</span><span class="p">)</span>
<span class="n">w</span> <span class="o">=</span> <span class="n">png</span><span class="o">.</span><span class="n">Writer</span><span class="p">(</span><span class="mf">3</span><span class="p">,</span> <span class="mf">2</span><span class="p">)</span>
<span class="n">w</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">p</span><span class="p">)</span> <span class="p">;</span> <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="more-colour">
<h3>More Colour<a class="headerlink" href="#more-colour" title="Permalink to this headline">¶</a></h3>
<p>A further colour example illustrates some of the manoeuvres you have to
perform in Python to get the pixel data in the right format.</p>
<p>Say we want to produce a PNG image with 1 row of 8 pixels, with all the
colours from a 3-bit colour system (with 1-bit for each channel;
such systems were common on 8-bit micros from the 1980s).</p>
<p>We produce all possible 3-bit numbers:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">range</span><span class="p">(</span><span class="mf">8</span><span class="p">)</span>
<span class="go">[0, 1, 2, 3, 4, 5, 6, 7]</span>
</pre></div>
</div>
<p>We can convert each number into an RGB triple by assigning bit 0 to
blue, bit 1 to red, bit 2 to green (the convention used by a certain
8-bit micro):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="nb">bool</span><span class="p">(</span><span class="n">x</span><span class="o">&amp;</span><span class="mf">2</span><span class="p">),</span> <span class="nb">bool</span><span class="p">(</span><span class="n">x</span><span class="o">&amp;</span><span class="mf">4</span><span class="p">),</span> <span class="nb">bool</span><span class="p">(</span><span class="n">x</span><span class="o">&amp;</span><span class="mf">1</span><span class="p">)),</span> <span class="n">_</span><span class="p">)</span>
<span class="go">[(False, False, False), (False, False, True), (True, False, False),</span>
<span class="go">(True, False, True), (False, True, False), (False, True, True), (True,</span>
<span class="go">True, False), (True, True, True)]</span>
</pre></div>
</div>
<p>(later on we will convert False into 0, and True into 255, so don&#8217;t
worry about that just yet).  Here we have each pixel as a tuple.  We
want to flatten the pixels so that we have just one row.  In other words
instead of [(R,G,B), (R,G,B), ...] we want [R,G,B,R,G,B,...].  It turns
out that <tt class="docutils literal"><span class="pre">itertools.chain(*...)</span></tt> is just what we need:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span><span class="o">*</span><span class="n">_</span><span class="p">))</span>
<span class="go">[False, False, False, False, False, True, True, False, False, True,</span>
<span class="go">False, True, False, True, False, False, True, True, True, True, False,</span>
<span class="go">True, True, True]</span>
</pre></div>
</div>
<p>Note that the <tt class="docutils literal"><span class="pre">list</span></tt> is not necessary, we can usually use the iterator
directly instead.  I just used <tt class="docutils literal"><span class="pre">list</span></tt> here so we can see the result.</p>
<p>Now to convert False to 0 and True to 255 we can multiply by 255
(Python use&#8217;s Iverson&#8217;s convention, so <tt class="docutils literal"><span class="pre">False==0</span></tt>, <tt class="docutils literal"><span class="pre">True==1</span></tt>).
We could do that with <tt class="docutils literal"><span class="pre">map(lambda</span> <span class="pre">x:255*x,</span> <span class="pre">_)</span></tt>. Or, we could use a
&#8220;magic&#8221; bound method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">map</span><span class="p">((</span><span class="mf">255</span><span class="p">)</span><span class="o">.</span><span class="n">__mul__</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span>
<span class="go">[0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255,</span>
<span class="go">255, 255, 0, 255, 255, 255]</span>
</pre></div>
</div>
<p>Now we write the PNG file out:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">=</span><span class="n">_</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">=</span><span class="nb">open</span><span class="p">(</span><span class="s">&#39;speccy.png&#39;</span><span class="p">,</span> <span class="s">&#39;wb&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">[</span><span class="n">p</span><span class="p">])</span> <span class="p">;</span> <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="reading">
<h2>Reading<a class="headerlink" href="#reading" title="Permalink to this headline">¶</a></h2>
<p>The basic strategy is to create a <a title="png.Reader" class="reference external" href="png.html#png.Reader"><tt class="xref docutils literal"><span class="pre">Reader</span></tt></a> object (a
<a title="png.Reader" class="reference external" href="png.html#png.Reader"><tt class="xref docutils literal"><span class="pre">png.Reader</span></tt></a> instance), then call its <tt class="xref docutils literal"><span class="pre">png.read()</span></tt> method
to extract the size, and pixel data.</p>
<div class="section" id="pngsuite">
<h3>PngSuite<a class="headerlink" href="#pngsuite" title="Permalink to this headline">¶</a></h3>
<p>The <a title="png.Reader" class="reference external" href="png.html#png.Reader"><tt class="xref docutils literal"><span class="pre">Reader()</span></tt></a> constructor can take either a filename, a file-like
object, or a sequence of bytes directly.  Here we use <tt class="docutils literal"><span class="pre">urllib</span></tt> to download
a PNG file from the internet.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">=</span><span class="n">png</span><span class="o">.</span><span class="n">Reader</span><span class="p">(</span><span class="nb">file</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s">&#39;http://www.schaik.com/pngsuite/basn0g02.png&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="go">(32, 32, &lt;itertools.imap object at 0x10b7eb0&gt;, {&#39;greyscale&#39;: True,</span>
<span class="go">&#39;alpha&#39;: False, &#39;interlace&#39;: 0, &#39;bitdepth&#39;: 2, &#39;gamma&#39;: 1.0})</span>
</pre></div>
</div>
<p>The <tt class="xref docutils literal"><span class="pre">png.read()</span></tt> method returns a 4-tuple.  Note that the pixels are
returned as an iterator (not always, and the interface doesn&#8217;t guarantee it;
the returned value might be an iterator or a sequence).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="n">_</span><span class="p">[</span><span class="mf">2</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span>
<span class="go">array(&#39;B&#39;, [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 0, 0, 0, 0,</span>
<span class="go">1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3])</span>
</pre></div>
</div>
<p>We have extracted the top row of the image.  Note that the row itself is
an <tt class="docutils literal"><span class="pre">array</span></tt> (see module <tt class="docutils literal"><span class="pre">array</span></tt>), but in general any suitable sequence
type may be returned by <tt class="xref docutils literal"><span class="pre">read()</span></tt>.  The values in the row are all
integers less than 4, because the image has a bit depth of 2.</p>
</div>
</div>
<div class="section" id="numpy">
<h2>NumPy<a class="headerlink" href="#numpy" title="Permalink to this headline">¶</a></h2>
<p><a class="reference external" href="http://numpy.scipy.org/">NumPy</a> is a package for scientific computing with Python.  It is not part
of a standard Python installation, it is
<a class="reference external" href="http://sourceforge.net/project/showfiles.php?group_id=1369&amp;package_id=175103">downloaded and installed separately</a>
if needed.  Numpy&#8217;s array manipulation facilities make it good for doing
certain type of image processing, and scientific users of NumPy may wish
to output PNG files for visualisation.</p>
<p>PyPNG does not have any direct integration with NumPy, but the basic
data format used by PyPNG, an iterator over rows, is fairly easy to get
into two- or three-dimensional NumPy arrays.</p>
<p>The code in this section is extracted from <tt class="docutils literal"><span class="pre">exnumpy.py</span></tt>, which is a
complete runnable example in the <tt class="docutils literal"><span class="pre">code/</span></tt> subdirectory of the source
distribution. Code was originally written by Mel Raab, but has been
hacked around since then.</p>
<div class="section" id="png-to-numpy-array-reading">
<h3>PNG to NumPy array (reading)<a class="headerlink" href="#png-to-numpy-array-reading" title="Permalink to this headline">¶</a></h3>
<p>The best thing to do (I think) is to convert each PyPNG row to a
1-dimensional numpy array, then stack all of those arrays together to
make a 2-dimensional array.  A number of features make this surprising
compact.  Say <cite>pngdata</cite> is the row iterator returned from
<a title="png.Reader.asDirect" class="reference external" href="png.html#png.Reader.asDirect"><tt class="xref docutils literal"><span class="pre">png.Reader.asDirect()</span></tt></a>.  The following code will slurp it into a
2-dimensional numpy array:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">image_2d</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">vstack</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">imap</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">uint16</span><span class="p">,</span> <span class="n">pngdata</span><span class="p">))</span>
</pre></div>
</div>
<p>Note that the use of <tt class="docutils literal"><span class="pre">numpy.uint16</span></tt>, above, means that an array with
data type <tt class="docutils literal"><span class="pre">numpy.uint16</span></tt> is created which is suitable for
bit depth 16 images.  Replace <tt class="docutils literal"><span class="pre">numpy.uint16</span></tt> with <tt class="docutils literal"><span class="pre">numpy.uint8</span></tt> to
create an array with a byte data type (suitable for bit depths up to 8).</p>
</div>
<div class="section" id="reshaping">
<h3>Reshaping<a class="headerlink" href="#reshaping" title="Permalink to this headline">¶</a></h3>
<p>For some operations it&#8217;s easier to have the image data in a
3-dimensional array.  This plays to NumPy&#8217;s strengths:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">image_3d</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">image_2d</span><span class="p">,</span>
                         <span class="p">(</span><span class="n">row_count</span><span class="p">,</span><span class="n">column_count</span><span class="p">,</span><span class="n">plane_count</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="numpy-array-to-png-writing">
<h3>NumPy array to PNG (writing)<a class="headerlink" href="#numpy-array-to-png-writing" title="Permalink to this headline">¶</a></h3>
<p>Reshape your NumPy data into a 2-dimensional array, then use the fact
that a NumPy array is an iterator over its rows:</p>
<div class="highlight-python"><pre>    pngWriter.write(pngfile,
                    numpy.reshape(image_3d, (-1, column_count*plane_count)))
</pre>
</div>
<p>Currently (writing on 2009-04-16) this generates a warning; this warning
appears to be a bug/limitation in NumPy, but it is harmless.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">PyPNG Code Examples</a><ul>
<li><a class="reference external" href="#writing">Writing</a><ul>
<li><a class="reference external" href="#a-ramp">A Ramp</a></li>
<li><a class="reference external" href="#a-little-message">A Little Message</a></li>
<li><a class="reference external" href="#a-palette">A Palette</a></li>
<li><a class="reference external" href="#colour">Colour</a></li>
<li><a class="reference external" href="#more-colour">More Colour</a></li>
</ul>
</li>
<li><a class="reference external" href="#reading">Reading</a><ul>
<li><a class="reference external" href="#pngsuite">PngSuite</a></li>
</ul>
</li>
<li><a class="reference external" href="#numpy">NumPy</a><ul>
<li><a class="reference external" href="#png-to-numpy-array-reading">PNG to NumPy array (reading)</a></li>
<li><a class="reference external" href="#reshaping">Reshaping</a></li>
<li><a class="reference external" href="#numpy-array-to-png-writing">NumPy array to PNG (writing)</a></li>
</ul>
</li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="ca.html"
                                  title="previous chapter">Why Use PyPNG?</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="png.html"
                                  title="next chapter">The png Module</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/ex.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="png.html" title="The png Module"
             >next</a> |</li>
        <li class="right" >
          <a href="ca.html" title="Why Use PyPNG?"
             >previous</a> |</li>
        <li><a href="index.html">PyPNG v0.0.11 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2009, David Jones.
      Last updated on 2010-01-05T10:31:12.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>